import React,{useState,useEffect} from 'react'
import {Button,message} from 'antd'
import '../style/activityInfo.less'
import ChooseList from './chooseList'
import Apply from '../card/apply'
import Vote from '../card/vote'
import LuckyOrRaffle from '../card/luckyOrRaffle'
import ScrambleTickets from '../card/scrambleTickets'
import SignIn from '../card/signIn'
import  {Url}  from '../../../Utils/services/url'
import { post } from '@/Utils/axios'
import { history } from 'umi'
export default function ActivityInfo(props:any) {
  const [sumbitForm,setSumbitForm] = useState(false)
  const [formData,setFormData] = useState({})
  const [chooseList,setChooseList]:Array<any> = useState([])
  var childNum= 0
  const [upDateType,setUpDateType] = useState('add')
  const activityList = [
    {
      value:1,
      label:'报名',
      component:<Apply form={sumbitForm} formData={formData} applyForm={changeFormData} changeState={changeState}></Apply>
    },
    {
      value:2,
      label:'抢票',
      component:<ScrambleTickets></ScrambleTickets>
    },
    {
      value:3,
      label:'签到',
      component:<SignIn></SignIn>
    },
    {
      value:4,
      label:'投票',
      component:<Vote form={sumbitForm} formData={formData} voteForm={changeFormData} changeState={changeState}></Vote>
    },
    {
      value:5,
      label:'抽奖',
      component:<LuckyOrRaffle></LuckyOrRaffle>
    }
  ]
  useEffect(()=>{
    let formData = {
      activityName:props.formData.activityName,
      activityContent:props.formData.activityContent,
      activitySite:props.formData.activitySite,
      activityOrganizers:props.formData.activityOrganizers,
      startDate:props.formData.activityDate[0].format('YYYY-MM-DD hh:mm:ss'),
      endDate:props.formData.activityDate[1].format('YYYY-MM-DD hh:mm:ss'),
      pictureKey:props.formData.pictureKey.imgKey,
      pictureUrl:props.formData.pictureKey.imgUrl,
      thumbnailPictureKey:props.formData.thumbnailPictureKey.imgKey,
      thumbnailPictureUrl:props.formData.thumbnailPictureKey.imgUrl,
      provinceCode:props.formData.address[0],
      cityCode:props.formData.address[1],
      scheduleVOS:props.formData.scheduleVOS !== undefined?props.formData.scheduleVOS : [],
      activityVOS:props.formData.activityVOS !== undefined?props.formData.activityVOS : []
    }
    console.log('父组件',formData)
    let arr = []
    if(props.formData.id !== undefined){
      formData = Object.assign({...formData},{activityBasicId:props.formData.activityBasicId})
    }
    if(props.formData.isSignUp == true){
      arr.push(1)
    }
    if(props.formData.isRobTickets == true){
      arr.push(2)
    }
    if(props.formData.isSignIn == true){
      arr.push(3)
    }
    if(props.formData.isVote == true){
      arr.push(4)
    }
    if(props.formData.isLuckyDraw == true){
      arr.push(5)
    }
    setChooseList(arr)
    setFormData(formData)
  },[props.formData])
  function chooseModel(list:any){
    setChooseList(list)
  }
  function sumbit(){
    changeState()
    setUpDateType('add')
  }
  function changeState(){
    setSumbitForm(true)
  }
  function changeFormData(form:any){
    childNum = childNum+1
    let obj:any = {
      ...formData
    }
    console.log('1234',obj)
    if(obj.activityBasicId){
      for(let item in obj.activityVOS){
        if(obj.activityVOS[item].activityType == form.activityType){
          obj.activityVOS[item] = {...form}
        }
      }
      let number = obj.activityVOS.filter((t:any)=>t.activityType == form.activityType)
      if(number.length == 0){
        obj.activityVOS.push(form)
      }
    } else {
      obj.activityVOS.push(form)
    }
    
    setFormData(obj)
    setSumbitForm(false)
    if(childNum == chooseList.length){
      upData(upDateType)
    }
  }
  function goBack(){
    const num = 1
    props.changeCreate(formData,num)
  }
  function saveDraft(){
    changeState()
    setUpDateType('draft')
  }
  function upData(type:string){
     let obj = {
      ...formData,
      isSignUp:chooseList.includes(1)?true:false,
      isRobTickets:chooseList.includes(2)?true:false,
      isSignIn:chooseList.includes(3)?true:false,
      isVote:chooseList.includes(4)?true:false,
      isLuckyDraw:chooseList.includes(5)?true:false,
    }
    post(type == 'add'?Url.addActivity:Url.saveDraft,obj).then(res =>{
      if(!res.code){
        message.success('操作成功')
        history.push({
          pathname:'/index/createActivity'
        })
        childNum = 0
      } else {
        message.error(res.message)
      }
    },err =>{
      console.log('error',err)
    })
  }
  /**子组件返回form信息 */
  return (
    <div>
      <div className="activity_info">
        <span className='activity_title'>选择活动</span>
        <ChooseList list={activityList} chooseData={chooseList} chooseModel={chooseModel} size={'large'}></ChooseList>
        <div style={{overflow:'auto',height:'calc(100vh - 380px)',marginTop:'15px'}}>
          {activityList.map((el:any)=>{
            if(chooseList.includes(el.value)){
              return (<div style={{background:'#ececec',padding:'15px'}}key={el.label}>{el.component}</div>)
            }
          })}
        </div>
      </div>
      <div className='button_group'>
          <Button onClick={sumbit} >发布</Button>
          <Button onClick={goBack}>上一步</Button>
          <Button >预览</Button>
          <Button onClick={saveDraft}>保存草稿</Button>
        </div>
    </div>
  )
}
